package com.example.littleSpider.myapplication;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.littleSpider.myapplication.activity.CityListActivity;
import com.example.littleSpider.myapplication.activity.MyEspeciallySaleActivity;
import com.example.littleSpider.myapplication.activity.ShoppingCartActivity;
import com.example.littleSpider.myapplication.base.BaseActivity;
import com.example.littleSpider.myapplication.fragment.EspeciallySaleFragment;
import com.example.littleSpider.myapplication.fragment.HomePageFragment;
import com.example.littleSpider.myapplication.fragment.PersonFragment;
import com.example.littleSpider.myapplication.fragment.VideoFragment;
import com.example.littleSpider.myapplication.myInterfaces.MethodInTitle;
import com.example.littleSpider.myapplication.view.TitleView;
import com.littlespider.mylibrary.view.WeChatCaptureActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

@RequiresApi(api = Build.VERSION_CODES.M)
public class MainActivity extends BaseActivity implements MethodInTitle{

    @BindView(R.id.title_view)
    TitleView mTitleView;
    @BindView(R.id.fragment)
    FrameLayout fragment;
    @BindView(R.id.rg_btn_one)
    RadioButton rg_btn_one;
    ImageView iv_right_one, iv_right_two;
    TextView tv_right_one, tv_right_two;

    List<Fragment> fragmentList = new ArrayList<>();
    HomePageFragment fragmentOne = new HomePageFragment();
    EspeciallySaleFragment fragmentTwo = new EspeciallySaleFragment();
    VideoFragment fragmentThree = new VideoFragment();
    PersonFragment fragmentFour = new PersonFragment();
    private FragmentManager manager;

    private int page = 0;
    private final int PAGE_ONE = 0;
    private final int PAGE_TWO = 1;
    private final int PAGE_THREE = 2;
    private final int PAGE_FOUR = 3;

    String url_path = "https://mobile.51ganjie.cn//appUser/login.do?userName=15172144011&pwd=111111&loginType=0";

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mTitleView.initMyTitle("你好, 我找老和尚", this);
        mTitleView.hideBack();
        mTitleView.showCity();
        mTitleView.hideTitleTextImage();
        iv_right_one = (ImageView) mTitleView.findViewById(R.id.iv_right_one);
        iv_right_two = (ImageView) mTitleView.findViewById(R.id.iv_right_two);
        tv_right_one = (TextView) mTitleView.findViewById(R.id.tv_right_one);
        tv_right_two = (TextView) mTitleView.findViewById(R.id.tv_right_two);
        fragmentList.add(fragmentOne);
        fragmentList.add(fragmentTwo);
        fragmentList.add(fragmentThree);
        fragmentList.add(fragmentFour);
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
//        transaction.add(R.id.fragment, fragmentList.get(0));
//        transaction.show(fragmentList.get(0));
//        transaction.commit();
        switchPage(0);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onStop() {
        super.onStop();
        for (int i = 0; i < fragmentList.size(); i++) {
            if (fragmentList.get(i).isAdded()){
                FragmentTransaction transaction = manager.beginTransaction();
//                transaction.remove(fragmentList.get(i));
//                Fragment fragment = manager.findFragmentById(R.id.fragment);
                transaction.remove(fragmentList.get(i));
//                transaction.commit();
//                transaction.detach(fragmentList.get(i));
//                fragmentList.get(i).
//                transaction.commit();
            }
        }
    }

    /**
     * 底部四个按钮的点击事件
     * @param view
     */
    @OnClick({R.id.rg_btn_one, R.id.rg_btn_two, R.id.rg_btn_three, R.id.rg_btn_four,})
    public void radioButtons(View view){
        switch (view.getId()){
            case R.id.rg_btn_one: //首页
                page = PAGE_ONE;
                switchPage(0);
                break;
            case R.id.rg_btn_two: //特卖
                page = PAGE_TWO;
//                startActivity(new Intent(this, FindJobActivity.class));
                switchPage(1);
                break;
            case R.id.rg_btn_three: //小视频
                page = PAGE_THREE;
                switchPage(2);
                break;
            case R.id.rg_btn_four: //个人中心
                page = PAGE_FOUR;
                switchPage(3);
                break;
            default:
                break;
        }
        changeTitle();
    }

    /**
     * 切换不同的fragment
     * @param pageNum
     */
    private void switchPage(int pageNum){
//        changeTitle();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment currentFragment = fragmentList.get(pageNum);
        //遍历fragment集合中的所有fragment
        for (Fragment fragment : fragmentList){
            //如果之前被添加过,就隐藏它
            if (fragment.isAdded()){
                transaction.hide(fragment);
            }
        }
        //当前要被切换到的fragment没有被添加时
        if (!currentFragment.isAdded()){
            transaction.add(R.id.fragment, currentFragment);
            transaction.show(currentFragment);
        }else {
            transaction.show(currentFragment);
        }
        transaction.commit();
    }

    /**
     * 改变标题栏
     */
    private void changeTitle(){
        mTitleView.hideImage();
        mTitleView.showTitleText();
        mTitleView.showRightTwo(true);
        switch (page){
            case PAGE_ONE:
                mTitleView.changeTitleText(getString(R.string.home_page));
                iv_right_one.setImageResource(R.drawable.spider_icon);
                iv_right_two.setImageResource(R.drawable.spider_icon);
                tv_right_one.setText(getString(R.string.scanner_once));
                tv_right_two.setText(getString(R.string.message));
                break;
            case PAGE_TWO:
                mTitleView.hideTitleText();
                mTitleView.showImage(R.drawable.pic_especially_sale);
                iv_right_one.setImageResource(R.drawable.icon_mine);
                iv_right_two.setImageResource(R.drawable.icon_shoppingcart);
                tv_right_one.setText(getString(R.string.my_especially_sale));
                tv_right_two.setText(getString(R.string.shopping_cart));
                break;
            case PAGE_THREE:
                mTitleView.changeTitleText(getString(R.string.little_video));
                mTitleView.showRightTwo(false);
                iv_right_one.setImageResource(R.drawable.spider_icon);
                iv_right_two.setImageResource(R.drawable.spider_icon);
                tv_right_one.setText(getString(R.string.scanner_once));
                tv_right_two.setText(getString(R.string.message));
                break;
            case PAGE_FOUR:
                mTitleView.changeTitleText(getString(R.string.person_center));
                iv_right_one.setImageResource(R.drawable.spider_icon);
                iv_right_two.setImageResource(R.drawable.spider_icon);
                tv_right_one.setText(getString(R.string.scanner_once));
                tv_right_two.setText(getString(R.string.message));
                break;
            default:
                break;
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.remove(fragmentOne);
        transaction.remove(fragmentTwo);
        transaction.remove(fragmentThree);
        transaction.remove(fragmentFour);
        transaction.commitAllowingStateLoss();
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == RESULT_OK){
            Intent intent = new Intent(this, WeChatCaptureActivity.class);
            startActivity(intent);
        }else {
            Toasty.warning(this, "你没有权限").show();
        }
    }

    @Override
    public void setGoBack() {
        Intent intent = new Intent(this, CityListActivity.class);
        startActivity(intent);
    }

    /**
     * 右边第一个按钮的点击事件
     */
    @Override
    public void onRightOne() {
        switch (page){
            case PAGE_ONE:
                break;
            case PAGE_TWO:
                Intent intent = new Intent(this, MyEspeciallySaleActivity.class);
                startActivity(intent);
                break;
            case PAGE_THREE:
                break;
            case PAGE_FOUR:
                break;
            default:
                break;
        }
    }

    @Override
    public void onRightTwo() {
        switch (page){
            case PAGE_ONE:
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED){
                    requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
                }else {
                    Intent intent = new Intent(this, WeChatCaptureActivity.class);
                    startActivity(intent);
                }
                break;
            case PAGE_TWO:
                Intent intent = new Intent();
                intent.setClass(this, ShoppingCartActivity.class);
                startActivity(intent);
                break;
            case PAGE_THREE:
                break;
            case PAGE_FOUR:
                break;
            default:
                break;
        }
    }

    @Override
    public void onTitleText() {

    }

}
